﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace DNFImagePacks2Manager.Helpers
{
    public class HashHelper
    {
        public static async Task<string> GetFileMD5(string file)
        {
            using var md5 = MD5.Create();
            using var stream = File.OpenRead(file);
            byte[] hashBytes = await md5.ComputeHashAsync(stream);

            StringBuilder sb = new(hashBytes.Length * 2);
            foreach (byte b in hashBytes)
            {
                sb.Append(b.ToString("X2"));
            }

            return sb.ToString().ToLowerInvariant();
        }

        public static async Task<string> GetFileSHA256(string file)
        {
            using var sha256 = SHA256.Create();
            using var stream = File.OpenRead(file);
            byte[] hashBytes = await sha256.ComputeHashAsync(stream);
            return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
        }
    }
}
